package com.erp.erp_project.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.erp.erp_project.core.exception.CustomerException;
import com.erp.erp_project.mapper.CustomMapper;
import com.erp.erp_project.model.Customer;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author the bright star
 */
@Service
@AllArgsConstructor
public class CustomService implements ICustomService{


    private CustomMapper customMapper;

    @Override
    public List<Customer> getAll() {
        QueryWrapper<Customer> wrapper=new QueryWrapper<>();
        wrapper.isEmptyOfWhere();
        return customMapper.selectList(wrapper);
    }

    @Override
    public void addCustomer(Customer customer) {
        QueryWrapper<Customer> wrapper=new QueryWrapper<>();
        wrapper.eq("full_name",customer.getFullName());
        Customer customer1=customMapper.selectOne(wrapper);
        if (customer1!=null){
            throw new CustomerException("已有该客户");
        }else{
            customMapper.insert(customer);
        }
    }

    @Override
    public void editCustomer(Customer customer) {
        Customer customer1=customMapper.selectById(customer.getCustomId());
        if (!customer.getFullName().equals(customer1.getFullName())){
            QueryWrapper<Customer> wrapper=new QueryWrapper<>();
            wrapper.eq("fullName",customer.getFullName());
            Customer customer2=customMapper.selectOne(wrapper);
            if (customer2!=null){
                throw new CustomerException("与其他客户重名");
            }
        }else{
            customMapper.updateById(customer);
        }
    }

    @Override
    public List<Customer> search(Integer type, String condition) {
        String str="custom_id";
        if (type==2){
            str="custom_name";
        }
        condition="%"+condition+"%";
        return customMapper.searchCustomer(str,condition);
    }

    @Override
    public Customer getCustomerById(Integer id) {
        return customMapper.selectById(id);
    }

    @Override
    public void deleteCustomer(List<Integer> list) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.in("custom_id",list);
        customMapper.delete(wrapper);
    }

    @Override
    public void setNote(String note, Integer id) {
        customMapper.updateNote(note,id);
    }


}
